home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap18 / exer1802 / MyStrings.java
Encoding:
Java Source  |  1997-04-20  |  1.2 KB  |  45 lines

  1. import java.io.*;
  2. import java.util.Vector;
  3.  
  4. public class MyStrings implements Serializable {
  5.  
  6.    private static final String FILE = "test";
  7.  
  8.    private Vector v = new Vector();
  9.  
  10.    public static void main(String[] args) {
  11.       MyStrings ms = new MyStrings();
  12.       ms.v.addElement("porcupine");
  13.       ms.v.addElement("turpentine");
  14.       write(ms);
  15.       read();
  16.    }
  17.  
  18.    private static void write(MyStrings ms) {
  19.       try {
  20.          FileOutputStream f = new FileOutputStream(FILE);
  21.          ObjectOutput s = new ObjectOutputStream(f);
  22.          s.writeObject(ms);
  23.          s.close();
  24.       } catch (IOException x) {
  25.          System.out.println(x.getMessage());
  26.       }
  27.    }
  28.  
  29.    private static void read() {
  30.       try {
  31.          FileInputStream f = new FileInputStream(FILE);
  32.          ObjectInput s = new ObjectInputStream(f);
  33.          MyStrings ms = (MyStrings)(s.readObject());
  34.          System.out.println(ms.v.elementAt(0));
  35.          System.out.println(ms.v.elementAt(1));
  36.          s.close();
  37.       } catch (IOException x) {
  38.          System.out.println(x.getMessage());
  39.       } catch (ClassNotFoundException x) {
  40.          System.out.println(x.getMessage());
  41.       }
  42.    }
  43.  
  44. }
  45.